﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using YJTTool.Core.ViewModels;

namespace YJTTool.Common.Controls
{
    public class DialogControl : UserControl
    {
        /// <summary>
        /// 静态构造函数，用于注册依赖属性  
        /// </summary>
        public DialogControl()
        {
            this.Loaded += (s, e) =>
            {
                var titleBar = this.Template.FindName("TitleBar", this) as Grid;
                if (titleBar != null)
                {
                    titleBar.MouseLeftButtonDown += Title_MouseLeftButtonDown;
                }
                if (this.DataContext != null)
                {
                    var vmodel = this.DataContext as DialogBaseViewModel;
                    if (vmodel != null)
                    {
                        vmodel.OnViewLoadedProtected(this);
                    }
                }
            };
        }

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(object), typeof(DialogControl));

        public object Title
        {
            get { return GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty DialogContentProperty =
            DependencyProperty.Register("DialogContent", typeof(object), typeof(DialogControl));

        public object DialogContent
        {
            get { return GetValue(DialogContentProperty); }
            set { SetValue(DialogContentProperty, value); }
        }

        private void Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Window.GetWindow((DependencyObject)sender)?.DragMove();
            }
        }
    }
}
